import { tasksList, tasksSaveByHis } from "$api/tasks"
import { arrJoin } from "$utils/splitObj"
import { getPagination, newgetDataGrid } from "$utils/table"
import dayjs from "dayjs"

export default {
  data() {
    return {
      taskDate: dayjs().subtract(1, "day").format("YYYY-MM-DD"),
      addForm: {
        taskDate: "",
      },
      dataGrid: {
        // 选中的id
        selectId: [],
        data: [],
        pagination: {
          // 每页数量
          pageSize: 1000,
          // 当前页数
          pageNum: 1,
          // 总页数
          sumPageNum: ""
        },
      }
    }
  },
  methods: {
    async save() {
      if (!this.dataGrid.selectId.length) {
        this.appMessage("请先选择任务", 1)
        return
      }
      const params = {
        taskDate: this.addForm.taskDate,
        ids: arrJoin(this.dataGrid.selectId)
      }
      await tasksSaveByHis(params)
      this.appMessage()
      setTimeout(() => {
        uni.navigateBack()
      }, 500);
    },
    async search() {
      const _form = getPagination({ taskDate: this.taskDate }, this.dataGrid)
      newgetDataGrid(this.dataGrid, tasksList, _form, false, data => {
        this.dataGrid.data = data.map(son => {
          const { remark } = son || {}
          const { selectBg, selctColor, endTime, startTime } = remark ? JSON.parse(remark) : {}
          return {
            ...son,
            endTime: endTime ? dayjs(endTime).format("YYYY-MM-DD HH:mm") : "",
            startTime: startTime ? dayjs(startTime).format("YYYY-MM-DD HH:mm") : "",
            selectBg, selctColor
          }
        })
      })
    },
    rowClick(item) {
      const index = this.dataGrid.selectId.indexOf(item.id)
      if (index === -1) {
        this.dataGrid.selectId.push(item.id)
      } else {
        this.dataGrid.selectId.splice(index, 1)
      }
    }

  }
}
